//
//  DrawView.m
//  绘制图形
//
//  Created by Justsoso on 16/4/6.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "DrawView.h"

@implementation DrawView



- (void)drawRect:(CGRect)rect
{
    [self drawGraphics1];
    [self drawGraphics2];
    [self drawGraphics3];
    [self drawGraphics4];
    [self drawGraphics5];
    [self drawGraphics6];
    [self drawGraphics7];
    [self drawGraphics8];
}

#pragma mark - 方法3: UIKit里封装的绘图功能(OC类)
- (void)drawGraphics8
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(125, 125)];
    
    [path addArcWithCenter:CGPointMake(125, 125) radius:100 startAngle:0 endAngle:M_PI_2 clockwise:YES];
    
    [path closePath];
    [[UIColor whiteColor] setStroke];
    [[UIColor redColor] setFill];
    path.lineWidth = 5;
    [path stroke];
    [path fill];
}


#pragma mark - 方法3: UIKit里封装的绘图功能(OC类)
- (void)drawGraphics7
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 200, 200) cornerRadius:100];
    [[UIColor yellowColor] setStroke];
    [[UIColor greenColor] setFill];
    path.lineWidth = 50;
    [path stroke];
    [path fill];
}


#pragma mark - 方法3: UIKit里封装的绘图功能(OC类)
- (void)drawGraphics6
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, 50, 200);
    CGContextAddQuadCurveToPoint(ctx, 150, 250, 250, 200);
    [[UIColor redColor] setStroke];
    [[UIColor greenColor] setFill];
    
    CGContextStrokePath(ctx);
//    CGContextClosePath(ctx);
//    CGContextFillPath(ctx);
    
}


#pragma mark - 方法3: UIKit里封装的绘图功能(OC类)
- (void)drawGraphics5
{
    /* 1. 创建路径 */
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(50, 100)];
    path.lineWidth = 10;
    [[UIColor redColor] setStroke];
    /* 2. 绘制路径 */
    [path stroke];
    
    /* 1. 创建路径 */
    UIBezierPath *path2 = [UIBezierPath bezierPath];
    [path2 moveToPoint:CGPointMake(50, 100)];
    [path2 addLineToPoint:CGPointMake(100, 100)];
    path2.lineWidth = 10;
    [[UIColor yellowColor] setStroke];
    /* 2. 绘制路径 */
    [path2 stroke];
}


#pragma mark - 方法3: UIKit里封装的绘图功能(OC类)
- (void)drawGraphics4
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(ctx, 50, 50);
    CGContextAddLineToPoint(ctx, 100, 50);
    CGContextAddLineToPoint(ctx, 100, 100);
    
    [[UIColor orangeColor] setStroke];
    
    CGContextSetLineWidth(ctx, 10);
    
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    
    CGContextStrokePath(ctx);
    
}
#pragma mark - 方法3: UIKit里封装的绘图功能(OC类)
- (void)drawGraphics3
{
    /* 贝塞尔路径 */
    
    /* 1. 创建路径 */
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(250, 200)];
    /* 2. 绘制路径 */
    [path stroke];
}

#pragma mark - 方法2: 在图形上下添加路径(省略了创建路径)
- (void)drawGraphics2
{
    /* 1. 得到图形上下文 (得到当前UIView对应的绘图环境) */
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    /* 2. 添加路径 */
    CGContextMoveToPoint(ctx, 50, 50);
    CGContextAddLineToPoint(ctx, 150, 200);
    /* 3. 绘制出来 (渲染) */
    CGContextStrokePath(ctx);
    
}

#pragma mark - 方法1: 在图形上下添加路径
- (void)drawGraphics1
{
    /* 1. 得到图形上下文 (得到当前UIView对应的绘图环境) */
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    /* 2. 添加路径 */
    CGMutablePathRef path = CGPathCreateMutable();
    /* 移动到某点 */
    CGPathMoveToPoint(path, NULL, 50, 50);
    /* 画条线到某点 */
    CGPathAddLineToPoint(path, NULL, 200, 200);
    
    /* 3. 把路径添加到图形上下中 */
    CGContextAddPath(ctx, path);
    
    /* 4. 绘制出来 (渲染) */
    CGContextStrokePath(ctx);
}
@end
